﻿using Bogus;
using MaterialDesignDemo.Command;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using static MaterialDesignDemo.UserControls.ItemListControl;
using static System.Net.Mime.MediaTypeNames;

namespace MaterialDesignDemo.UserControls
{
    /// <summary>
    /// GridViewPage.xaml 的交互逻辑
    /// </summary>
    public partial class GridViewPage : UserControl
    {

        ViewModel viewModel;
        public GridViewPage()
        {
            Randomizer.Seed = new Random(111);

            InitializeComponent();

            viewModel = new ViewModel(Person.FakeMany(100));
            this.DataContext = viewModel;
           
        }

       
    }

    public class ViewModel : ObservableObject, INotifyPropertyChanged
    {
        public ViewModel(IEnumerable<Person> persons)
        {
            this.persons = new List<Person>(persons);

            PersonDisplay = this.persons.Take(pageSize);


            AddPersonCommonand = new RelayCommand(AddPerson);
            GoToPageCommonand = new RelayCommand(GoToPage);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        List<Person> persons;
        public const int pageSize = 15;

        //用于展示的信息
        private IEnumerable<Person> personDisplay;
        public IEnumerable<Person> PersonDisplay
        {
            get { return personDisplay; }
            set
            {
                if (personDisplay != value)
                {
                    personDisplay = value;
                    OnPropertyChanged();
                }
            }
        }
        //筛选文本
        private string filterText;
        public string FilterText
        {
            get { return filterText; }
            set
            {
                if (filterText != value)
                {
                    filterText = value;
                    OnPropertyChanged();
                    //CollectionView.Refresh();
                    RefreshDisPerson();
                }
            }
        }

        public RelayCommand AddPersonCommonand { get; set; }
        public RelayCommand GoToPageCommonand { get; set; }

        /// <summary>
        /// 添加人员
        /// </summary>
        public void AddPerson()
        {
            persons.Add(Person.FakeOne());
        }
        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="pageNumber"></param>
        public void GoToPage(Object pageNumber)
        {
            if (string.IsNullOrEmpty(pageNumber.ToString()))
                return;
            var maxPageNum = persons.Count % pageSize > 0 ? persons.Count / pageSize + 1 : persons.Count / pageSize;
            var p = Convert.ToInt32(pageNumber);
            if (p > maxPageNum)
            {
                MessageBox.Show("超过最大页数");
                return;
            }
            PersonDisplay = this.persons.Skip(pageSize * (p - 1)).Take(pageSize);
           
        }
        /// <summary>
        /// 刷新页面
        /// </summary>

        public void RefreshDisPerson()
        {
            if (!string.IsNullOrEmpty(FilterText))
            {
                PersonDisplay = PersonDisplay.Where(s => s.Name.Contains(FilterText) || s.Address.Contains(FilterText));
            }
        }
    }
}
